/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// Interpreter.h: interface for the CInterpreter class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_INTERPRETER_H__A4C560F6_231D_4F74_AEE3_F9B3E7F87B14__INCLUDED_)
#define AFX_INTERPRETER_H__A4C560F6_231D_4F74_AEE3_F9B3E7F87B14__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "ssi_stream.h"
#include "ssi_defines.h"
#include "ssi_string.h"
#include "ssi_engine.h"
#include "ssi_input.h"
#include "ssi_status.h"	// Added by ClassView
#include "ssi_variable.h"	// Added by ClassView
#include "ssi_ptrarray.h"	// Added by ClassView
#include "ssi_functiondecl.h"
#include "ssi_function.h"
#include "ssi_classdecl.h"
#include "ssi_scriptline.h"
#include "ssi_scriptstatementlist.h"
#include "ssi_componentparser.h"

// The number of Bracket types for find_special (eg: [],() = 2 types
#define NUM_BRACKETS 2

// Execution Flags
#define EXEC_NO   0   // Do not Execute
#define EXEC_GO   1   // Execute
#define EXEC_PRE  2   // Execute for Preprocessing

class CEngine;
class SSI_SCRIPTDLL_API CInterpreter  
{
public:
	void SetOutDisplay(ssi_stream* NewDisp);
	void ClearReturnState();
	SSI_BOOL GetReturnState();
	CVariable GetFunctionReturnVar();
	CStatus ProcessUserFunction(CFunction& aFunc, CFunctionDecl& aDecl);

	CStatus DeclareUserClass(String& inLine, CClassDecl& newDecl);
	CStatus DefineUserClass(String& inLine, CClassDecl& newDecl);

	CStatus DeclareUserFunction(String& inLine, CFunctionDecl& newDecl);
	CStatus DefineUserFunction(String& inLine, CFunctionDecl& newDecl);
	CStatus CreateUserFunction(const String& inLine);
	
	CStatus InterpretFile(String& FileName);
	CStatus EvaluateKeyword(const String& inLine);
	SSI_BOOL gInitialized;
	//CStatus CheckFlowClose(String& sInput, CFlowStruct &aFlow);
	//CStatus ProcessElse(String& Return, CFlowStruct& aFlow);
	CStatus ProcessFlow(String& Return, CFlowStruct &aFlow, SSI_BOOL bEval = 1);
	
  //void SetLastFlow(CFlowStruct* In);
	CFlowStruct* LastFlow;
	
  CStatus FindEndOfFlow(const String& FirstString, CFlowStruct& Struct);
	//SSI_INT64 BCtr;
	CStatus EvaluateFlow(const String& In, CFlowStruct &InFlow);
	SSI_BOOL GetNextLine(CScriptLine &Ret);
	SSI_BOOL GetCurrLine(CScriptLine &Ret);

	CStatus Itemize(const String& In, String& Left, String& Right, String Delims);
  DimNode GetArrayDimensions(const String& Input, String& Output);
	CStatus Bracketize(const String& In, String& Left, String& Right);
  CStatus Parenthesize(const String& In, String& Left, String& Right);
	CStatus EvaluateFunction(String& Input, String& sFuncName, CVariable& RetVar);
	CStatus Evaluate(const String& inLine, CVariable &RetVar);

	//CStatus EvaluateLevel(SSI_INT64 Level, const String& Input, String& RetString, CVariable &RetVar);

	static SSI_INT64 GetVariableType(const String& In);
  static String GetVariableString(SSI_INT64 In);
  virtual CStatus CheckNewVariable(String Line, CVariable& NewVar); // No reference
	virtual CStatus CheckNewVariable(ComponentItor Itor, CComponentParser& aParser, CVariable& NewVar);
	void SetEngine(CEngine* Eng);
	SSI_BOOL Interpret(const String& Line, ScriptNode* InNode);
	CInterpreter(CInput* In);
	virtual ~CInterpreter();

protected:
	virtual CStatus DoFunctionReturn(const String& Input);
	virtual SSI_BOOL DeComment(String& In);

  String COMMENT_INDICATOR;
  String COMMENT_START;
  String COMMENT_STOP;
  String ESCAPE_INDICATOR;
  String BEGIN_STRING;
  String END_STRING;

protected:
	ssi_stream* out;
	ssi_stream* in;   // this needs to be included for later upgrades
private:
  CVariable FunctionReturnVariable;
	SSI_BOOL bFunctionReturnState;
	SSI_BOOL bStopFlag;

	CInput* Input;
	SSI_INT64 find_special(const String& Line, const String& OpSet, String& Ret);
  CStatus GLOBAL_RetStat;
  String RetMess;

  StringTree StartBrackList;
  StringTree StopBrackList;
	//BreakpointTree Breakpoints;

  CEngine* Engine;
public:
	virtual CStatus Parameterize(const String& Params, VarTree& Ret, String Toks = "(),");
	virtual CStatus Parameterize(const String &Params, StringTree& Ret, String Toks = "(),");
	CStatus CreateUserClass(const String& inLine);
	SSI_BOOL ChopEnder(String& Input);
	CStatus DoClass(String& VarName, CVariable& RetVar, VarTree &ClassVars);
	SSI_BOOL IsClass(String& VarName);
	void SeparateToStatements(CScriptLine& InLine, CScriptStatementList& theList);
	ScriptStatementNode* GetNextStatement(CScriptStatement& TheStatement);
	CScriptStatementList* StatementList;
	SSI_BOOL IsClassFunction(String& TestString);
	CStatus BuildFunction(const String& Input, CFunction& theFunc);
	SSI_BOOL DisplayError(CStatus& aStat, String& sLineNum);
	SSI_BOOL DisplayError(CStatus& aStat, const char* sLineNum){String s = sLineNum; return DisplayError(aStat, s);};
	SSI_BOOL DisplayMessage(String& sLineNum, String& Message);
	SSI_BOOL DisplayMessage(String& sLineNum, const char* Message){String s = Message; return DisplayMessage(sLineNum, s);};
	CStatus Evaluate(CComponentParser& aParser, CVariable& RetVar);

	SSI_BOOL GetStopState(void);
	void SetStopFlag(void);
	SSI_INT64 QueryInput(void);
	//SSI_INT64 AddBreakpoint(CBreakpoint& newBreakpoint);
	//SSI_INT64 RemoveBreakpoint(CBreakpoint& inBreakpoint);
	//SSI_INT64 CheckBreakpoint(CBreakpoint& inBreakpoint);
	CStatus GetFile(const String& FileName, String& LocFile);
  CEngine* GetEngine(void);
};

#endif // !defined(AFX_INTERPRETER_H__A4C560F6_231D_4F74_AEE3_F9B3E7F87B14__INCLUDED_)



